﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Threading;
using System.Threading;
using Global;
using Clipper.BLL.DetectChain;
using Communication;

namespace Clipper.ClipperUserControl
{
    /// <summary>
    /// UserCheckSelf.xaml 的交互逻辑
    /// </summary>
    public partial class UserCheckSelf : UserControl
    {
        bool[] checkStatus = new bool[8];
        public UserCheckSelf()
        {
            InitializeComponent();
        }
        #region 自检
        /// <summary>
        /// 自检
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSelfCheck_Click(object sender, RoutedEventArgs e)
        {
            prgSelfCheck.Value = 0;
            //DetectChainHelper.GetTemperature();
            BackgroundWorker backWorker = new BackgroundWorker();
            backWorker.WorkerReportsProgress = true;
            prgSelfCheck.Visibility = Visibility.Visible;
            btnSelfCheck.Visibility = Visibility.Hidden;
            btnCancel.Visibility = Visibility.Visible;
            txtSelfCheck.Text = UIHelper.GetResourceText("WaitSelfCheck");

            //自检计时
           
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 1000;
            string strCheckTime = txtSelfCheckTime.Text;
            int times = 0;
            
            timer.Elapsed += (ss, ee) =>
            {
                times++;
                this.Dispatcher.Invoke(DispatcherPriority.Normal,
                new Action(() =>
                {
                    txtSelfCheckTime.Text = strCheckTime + times;
                }));
            };

            backWorker.DoWork += (ss, ee) =>
            {

             //   ArmMoveControl.InitArmMoveValue(localtionModel.GetModelList(""));

                //杯子复位
                DeliveryCupControl.AllReset();
                //简单清杯子
                DeliveryCupControl.AllResetCleanCup();
                //DeliveryCupControl.AllReset();
                //杯子初始化
                DeliveryCupControl.Init();
                checkStatus[0] = true;
                backWorker.ReportProgress(20);

                ArmMoveControl.ResetAllAxis();
                //臂初始化
                ArmMoveControl.Init(ArmMoveControl.clearDistance);
                checkStatus[1] = true;
                backWorker.ReportProgress(40);
                
                if(Can.ReagentTemp[0] > 0 && Can.ReagentTemp[0] < 4000)
                {
                    checkStatus[2] = true;
                }
                backWorker.ReportProgress(50);
                //样本系统 及 急诊 传感器的检测
                checkStatus[3] = true;
                backWorker.ReportProgress(60);

                //孵育系统 温度是否有效

                if (Can.CupTemp[0] > 0 && Can.CupTemp[0] < 4000)
                {
                    checkStatus[4] = true;
                }
                backWorker.ReportProgress(70);


                //洗涤系统 泵阀 是否正常，通过检测针过水 检测
                checkStatus[5] = true;
                backWorker.ReportProgress(80);

                //光电检测系统 查看各通道关检测值是否属于正常范围
                checkStatus[6] = true;
                backWorker.ReportProgress(90);

                //其它相关传感器检测
                checkStatus[7] = true;
                backWorker.ReportProgress(100);

            };
            backWorker.ProgressChanged += (ss, ee) =>
            {
                prgSelfCheck.Value = ee.ProgressPercentage;
                switch (ee.ProgressPercentage)
                {
                    case 20:
                        lbl0.Visibility = Visibility;
                        break;
                    case 40:
                        lbl1.Visibility = Visibility;
                        break;
                    case 50:
                        if (checkStatus[2])
                        lbl2.Visibility = Visibility;
                        break;
                    case 60:
                        if (checkStatus[3])
                            lbl3.Visibility = Visibility;
                        break;
                    case 70:
                        if (checkStatus[4])
                            lbl4.Visibility = Visibility;
                        break;
                    case 80:
                        if (checkStatus[5])
                            lbl5.Visibility = Visibility;
                        break;
                    case 90:
                        if (checkStatus[6])
                            lbl6.Visibility = Visibility;
                        break;
                    case 100:
                        if (checkStatus[7])
                            lbl7.Visibility = Visibility;
                        ServiceProvider.SelfCheckPass = true;
                        ServiceProvider.Monitor.IsCheckSelfSuccess = Model.ComponentStateEnum.NORMAL;
                        break;
                }
            };
            backWorker.RunWorkerCompleted += (ss, ee) =>
            {
                //自检通过
                if (ServiceProvider.SelfCheckPass)
                {
                    txtSelfCheck.Text = UIHelper.GetResourceText("SelfCheckComplete");
                    prgSelfCheck.Visibility = Visibility.Hidden;
                    btnSelfCheck.Visibility = Visibility.Visible;
                    btnCancel.Visibility = Visibility.Hidden;
                }
                timer.Stop();

            };
            timer.Start();
            backWorker.RunWorkerAsync();
        }
        private void ClearCanal()
        {
         
        }
        private void ClearCanalArm(Communication.ArmControl arm)
        {
            //arm.WaitStatus(arm.Reset());
         
        }
        #endregion

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
